package dq.study.datasource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author dongqun
 * @version 1.0
 * @description 类的功能描述
 * @date 2019/9/23 11:18
 */
public class Test {
    public static void main(String[] args){
        class MyThread implements Runnable{
            public void run() {
                executeSQL();
            }
        }

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.submit(new MyThread());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.submit(new MyThread());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.submit(new MyThread());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.submit(new MyThread());
    }




    private static synchronized void executeSQL(){
        Connection conn = null;
        PreparedStatement statement = null;
        try {
            conn = MyDataSource.getDataSource().getConnection();
            statement = conn.prepareStatement("select * from apss_base_user where username like ?");
            statement.setObject(1,"%dong%");
            ResultSet set = statement.executeQuery();
            System.out.println("--------------------------------------------------------------");
            System.out.println("用户id        用户名称        用户昵称        用户手机        用户邮箱");
            System.out.println("--------------------------------------------------------------");
            while (set.next()){
                Long userid  = set.getLong("user_id");
                String username = set.getString("username");
                String nickname = set.getString("nickname");
                String phone = set.getString("phone");
                String email = set.getString("email");
                System.out.println(userid+"    "+username+"    "+nickname+"    "+phone+"    "+email);
                System.out.println("--------------------------------------------------------------");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
